import axios from 'axios';
import { REGIST } from './mutation_type';
import { ERR_CODE } from '../common/constant';
import { renderMsg } from '../common/util';

const initState = {
    user: '',
    pwd: '',
    repwd: '',
    msg: '',
    isAuth: false,
    type: '',
};

export function user (state=initState,action){
  switch(action.type){
		case REGIST.REGIST_ERROR:
			return {
				...state,
				...action.paylod
			};
		break;
		case REGIST.CLEAR_MSG:
			return {
				...state,
				msg:''
			}
		break;
    default:
        return state;
    break;
  }
}

export function errorMsg (errObj) {
  return {
    type: REGIST.REGIST_ERROR,
    paylod: {
        msg: errObj.msg
    }
  }
}

export function clearMsg () {
	return {
		type: REGIST.CLEAR_MSG
	}
}

export function regist ({ user, pwd, repwd, type }) {
  if(!user || !pwd || !type){
    return errorMsg(renderMsg(ERR_CODE.VALIDATE_ERR,'用户名或密码能为空'));
  }
  if( pwd !== repwd) {
    return errorMsg(renderMsg(ERR_CODE.VALIDATE_ERR,'密码和确认密码不一致'));
  }
  return dispatch => {
		axios.get('/user').then(res=>{
			console.log(res);
		})
  }
}